package com.wt.admin.controller.ai;


import com.aizuda.easy.security.domain.Rep;
import com.aizuda.easy.security.domain.Req;
import com.wt.admin.domain.dto.ai.AgentsInfoDTO;
import com.wt.admin.domain.dto.ai.ChatDTO;
import com.wt.admin.domain.dto.ai.ChatModelContentDTO;
import com.wt.admin.domain.vo.ai.*;
import com.wt.admin.domain.vo.sys.UserVO;
import com.wt.admin.service.ai.ChatProxyService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;


@RestController
@RequestMapping("chat")
public class ChatController {

    @Resource
    private ChatProxyService chatProxyService;


    @PostMapping("agentsList")
    public Rep<List<AgentsInfoVO>> agentsList(@RequestBody Req<AgentsInfoDTO, UserVO> req) {
        return Rep.ok(chatProxyService.agentsList(req.getData()));
    }

    @PostMapping("modeContentList")
    public Rep<List<ChatModelContentVO>> modeContentList(@RequestBody Req<ChatModelContentDTO, UserVO> req) {
        return Rep.ok(chatProxyService.getChatModelContentService().modeContentList(req.getData(), req.getUser()));
    }

    @PostMapping("modeContentDel")
    public Rep<ChatModelContentVO> modeContentDel(@RequestBody Req<ChatModelContentDTO, UserVO> req) {
        return Rep.ok(chatProxyService.modeContentDel(req.getData()));
    }

    @PostMapping("modeContentEdit")
    public Rep<ChatModelContentVO> modeContentEdit(@RequestBody Req<ChatDTO, UserVO> req) {
        return Rep.ok(chatProxyService.modeContentEdit(req.getData(), req.getUser()));
    }

    @PostMapping("modeContentInfo")
    public Rep<ChatModelContentVO> modeContentInfo(@RequestBody Req<ChatModelContentDTO, UserVO> req) {
        return Rep.ok(chatProxyService.modeContentInfo(req.getData(), req.getUser()));
    }

    @PostMapping("question")
    public Flux<String> question(@RequestBody Req<ChatDTO, UserVO> req) {
        final StringBuilder msg = new StringBuilder();
        return chatProxyService.question(req.getData(), req.getUser())
                .stream()
                .content()
                .doOnNext(msg::append)
                .doOnError(error -> msg.setLength(0))
                .doOnComplete(() -> chatProxyService.reply(msg.toString(),req.getData(),req.getUser()));
    }


}
